<template>
  <div class="choose-seat">
    <!-- 头部导航 -->
    <div class="header fixed-header">
      <div class="back-btn" @click="goBack">‹</div>
      <div class="cinema-name">{{resultData.cinemaName}}</div>
    </div>
    <!-- 座位状态说明 -->
    <div class="seat-legend">
      <div class="legend-item">
        <div class="seat-icon sold"></div>
        <span>已售</span>
      </div>
      <div class="legend-item">
        <div class="seat-icon available"></div>
        <span>可选</span>
      </div>
      <div class="legend-item">
        <div class="best-view-icon"></div>
        <span>最佳观影区</span>
      </div>
    </div>

    <!-- 屏幕 -->
    <div class="screen-area">
      <div class="screen">{{resultData.playHall}}号厅</div>
    </div>

    <!-- 座位区域 -->
    <div class="seat-area">
      <div class="row-numbers">
        <div v-for="row in 10" :key="row" class="row-number">{{ row }}</div>
      </div>
      <div class="seats-container">
        <div v-for="row in 10" :key="row" class="seat-row">
          <div 
            v-for="seat in 10" 
            :key="`${row}-${seat}`"
            :class="['seat', getSeatClass(row, seat)]"
            @click="toggleSeat(row, seat)"
          >
            <span v-if="isSeatSelected(row, seat)" class="seat-number">{{ row }}-{{ seat }}</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 最佳观影区标识 -->
    <div class="best-view-area"></div>

    <!-- 底部信息 -->
    <div class="bottom-info">
      <div class="warning">
        <span class="warning-icon">🔊</span>
        <span>影城提供免费3D眼镜，出场请及时归还。</span>
      </div>
      
      <div class="movie-info">
        <h3>{{resultData.filmName}}</h3>
        <div class="movie-details">
          <span class="date">{{formatScheduleDate}} {{resultData.startTime}}-{{resultData.endTime}}</span>
          <span class="format">国语2D</span>
        </div>
      </div>

      <button class="confirm-btn" :disabled="selectedSeats.length === 0" @click="goToPurchase()">
        {{ selectedSeats.length === 0 ? '请先选座' : '确认选座' }}
      </button>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, getCurrentInstance } from 'vue'
import api from '@/utils/http'

const global=getCurrentInstance().appContext.config.globalProperties;

// 座位数据 - 10排x10座
const resultData = ref({})
const seatData = ref({})

// 选中的座位
const selectedSeats = ref([])

// 格式化排片日期显示
const formatScheduleDate = computed(() => {
  if (!resultData.value.sliceArrangementDate) {
    return '今天'
  }
  
  const scheduleDate = new Date(resultData.value.sliceArrangementDate)
  const today = new Date()
  
  // 重置时分秒，只比较日期
  today.setHours(0, 0, 0, 0)
  scheduleDate.setHours(0, 0, 0, 0)
  
  const diffTime = scheduleDate.getTime() - today.getTime()
  const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24))
  
  const month = scheduleDate.getMonth() + 1
  const day = scheduleDate.getDate()
  
  if (diffDays === 0) {
    return `今天 ${month}月${day}日`
  } else if (diffDays === 1) {
    return `明天 ${month}月${day}日`
  } else if (diffDays === 2) {
    return `后天 ${month}月${day}日`
  } else if (diffDays > 0) {
    return `${month}月${day}日`
  } else {
    return `${month}月${day}日`
  }
})

// 初始化座位数据
const initSeats = () => {
  const seats = {}
  for (let row = 1; row <= 10; row++) {
    for (let seat = 1; seat <= 10; seat++) {
      const seatKey = `${row}-${seat}`
      seats[seatKey] = {
        row,
        seat,
        status: resultData.value.soldList.includes(seatKey) ? 'sold' : 'available', // sold: 已售, available: 可选, selected: 已选
        isBestView: row >= 4 && row <= 6 && seat >= 3 && seat <= 8 // 最佳观影区
      }
    }
  }
  seatData.value = seats
}

const getSeatClass = (row, seat) => {
  const seatKey = `${row}-${seat}`
  const seatInfo = seatData.value[seatKey]
  if (!seatInfo) return 'available'
  
  const classes = [seatInfo.status]
  if (seatInfo.isBestView && seatInfo.status === 'available') {
    classes.push('best-view')
  }
  return classes.join(' ')
}

// 切换座位选择
const toggleSeat = (row, seat) => {
  const seatKey = `${row}-${seat}`
  const seatInfo = seatData.value[seatKey]
  
  if (!seatInfo || seatInfo.status === 'sold') return
  
  if (seatInfo.status === 'selected') {
    // 取消选择
    seatInfo.status = 'available'
    selectedSeats.value = selectedSeats.value.filter(s => s !== seatKey)
  } else if (seatInfo.status === 'available') {
    // 选择座位
    seatInfo.status = 'selected'
    selectedSeats.value.push(seatKey)
  }
}

// 判断座位是否被选中
const isSeatSelected = (row, seat) => {
  const seatKey = `${row}-${seat}`
  return seatData.value[seatKey]?.status === 'selected'
}

// 返回上一页
const goBack = () => {
  global.$router.back()
}

const goToPurchase = () => {
  global.$router.push({
    path: '/moviePayOrder',
    query: {
      showtimeId: global.$route.query.showtimeId,
      seatList: selectedSeats.value
    }
  })
}

onMounted(() => {
  const showtimeId = global.$route.query.showtimeId
  api.get('/movieScheduling/getChooseSeat/'+showtimeId).then(res => {
    if(res.data){
      resultData.value = res.data
      initSeats()
    }
  })
})
</script>

<style scoped>
.choose-seat {
  background: #f5f5f5;
  min-height: 100vh;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  display: flex;
  flex-direction: column;
}

/* 头部导航 */
.header {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px 16px;
  background: white;
  border-bottom: 1px solid #eee;
  position: relative;
}

.back-btn {
  position: absolute;
  left: 16px;
  font-size: 24px;
  color: #333;
  cursor: pointer;
  padding: 4px;
}

.cinema-name {
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.menu-dots, .location-icon {
  font-size: 18px;
  color: #333;
  cursor: pointer;
  padding: 4px;
}

.tip-bar {
  display: flex;
  align-items: center;
  padding: 8px 16px;
  background: white;
  border-bottom: 1px solid #eee;
}

.close-btn {
  font-size: 18px;
  color: #999;
  margin-right: 12px;
  cursor: pointer;
}

.tip-text {
  font-size: 14px;
  color: #333;
}

.wechat-invite {
  display: flex;
  align-items: center;
  margin-left: auto;
  font-size: 14px;
  color: #52c41a;
  cursor: pointer;
}

.wechat-icon {
  margin-right: 4px;
}

.arrow {
  margin-left: 4px;
  color: #999;
}

.seat-legend {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: white;
  gap: 24px;
  margin-bottom: 8px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: #666;
}

.seat-icon {
  width: 20px;
  height: 20px;
  border-radius: 4px;
  border: 1px solid #ddd;
}

.seat-icon.sold {
  background: #ff4757;
  border-color: #ff4757;
}

.seat-icon.available {
  background: white;
  border-color: #ddd;
}

.seat-icon.unavailable {
  background: #f0f0f0;
  border-color: #ddd;
}

.best-view-icon {
  width: 20px;
  height: 20px;
  border: 2px dashed #ff6b35;
  border-radius: 4px;
}

.screen-area {
  display: flex;
  justify-content: center;
  padding: 20px;
  background: white;
}

.screen {
  background: #f0f0f0;
  padding: 8px 40px;
  border-radius: 20px;
  font-size: 14px;
  color: #666;
  text-align: center;
}

.seat-area {
  display: flex;
  background: white;
  padding: 20px 16px;
  gap: 8px;
  flex: 1;
  margin-bottom: 8px;
}

.row-numbers {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding-top: 4px;
}

.row-number {
  width: 20px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: #999;
  background: #f8f8f8;
  border-radius: 4px;
}

.seats-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow-x: auto;
}

.seat-row {
  display: flex;
  gap: 6px;
  justify-content: center;
}

.seat {
  width: 28px;
  height: 28px;
  border-radius: 4px;
  border: 1px solid #ddd;
  background: white;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 8px;
  color: white;
  font-weight: bold;
}

.seat.available:hover {
  border-color: #ff5722;
  transform: scale(1.1);
}

.seat.sold {
  background: #ff4757;
  border-color: #ff4757;
  cursor: not-allowed;
}

.seat.selected {
  background: #ff5722;
  border-color: #ff5722;
}

.seat.best-view {
  border: 2px dashed #ff6b35;
  border-color: #ff6b35;
}

.seat-number {
  font-size: 7px;
  line-height: 1;
  text-align: center;
}

.bottom-info {
  background: white;
  padding: 16px;
  margin-top: auto;
  border-top: 1px solid #eee;
}

.warning {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px;
  background: #fff7e6;
  border-radius: 8px;
  margin-bottom: 16px;
  font-size: 12px;
  color: #fa8c16;
}

.movie-info h3 {
  font-size: 18px;
  color: #333;
  margin: 0 0 8px 0;
}

.movie-details {
  font-size: 14px;
  color: #666;
  margin-bottom: 16px;
}

.date {
  margin-right: 12px;
}

.seat-recommendation {
  margin-bottom: 20px;
}

.recommendation-title {
  font-size: 14px;
  color: #333;
  margin-bottom: 8px;
}

.recommendation-options {
  display: flex;
  gap: 12px;
}

.option {
  padding: 6px 12px;
  background: #f8f8f8;
  border-radius: 16px;
  font-size: 12px;
  color: #666;
  cursor: pointer;
}

.option:hover {
  background: #e6f7ff;
  color: #1890ff;
}

.confirm-btn {
  width: 100%;
  padding: 14px;
  background: #ff5722;
  color: white;
  border: none;
  border-radius: 25px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}

.confirm-btn:disabled {
  background: #ffcdd2;
  cursor: not-allowed;
}

.confirm-btn:not(:disabled):hover {
  background: #e64a19;
}
.fixed-header {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 1000;
  background: white;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
</style>
